package com.bdqn.jpayhymeleaf.contorller;

import com.bdqn.jpayhymeleaf.entity.User;
import com.bdqn.jpayhymeleaf.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 2024-09-20
 * @Author:T369曹任柱
 */
@RestController
public class LoginController {
    @Resource
    private IUserService userService;



    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request){
        User user=userService.login(usrName,usrPassword);
        if (user!=null){
            request.getSession().setAttribute("loginUser",user);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或账号密码错误");
            return "login";
        }
    }


    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
}
